package com.coffeeloversupportlibrary.coffeelover.mercury.classify;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.classify.adapter.CommentAdapter;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.CoffeeBeanSearchList;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Comment;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;


import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/13 14:13
 * @projectName TurningCoffee
 * @className DetailedActivity.java
 * @description TODO
 */
public class DetailedActivity extends AppCompatActivity {
    private ImageView iv_coffee, star[];
    private TextView tv_headname, tv_name, tv_borigin, tv_brand, tv_baking, tv_type, tv_score, tv_introduce, tv_brief;
    private LinearLayout ll_want, ll_drinked, ll_favorite;
    private ListView  lv_comment;

    private CoffeeBeanSearchList.DataDTO.ItemsDTO coffee = new CoffeeBeanSearchList.DataDTO.ItemsDTO();

    private List<Comment.DataDTO.CommentsDTO> comments;
    private CommentAdapter adapter;

    private HttpUtils httpUtils;
    private JsonUtils jsonUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classify_activity_detailed);

        initViews();
        setListener();
        setData();
    }


    void setListener(){
        DetailedOnClickListener listener = new DetailedOnClickListener();
        ll_favorite.setOnClickListener(listener);
        ll_drinked.setOnClickListener(listener);
        ll_want.setOnClickListener(listener);

    }

    void setStar(int count){
        for(int i = 0; i < 5; i++){
            star[i].setImageResource(R.drawable.star0);
        }
        for (int i = 0; i<count; i++){
            star[i].setImageResource(R.drawable.star1);
        }
    }

    class DetailedOnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.ll_wantdrinkcfy:
                    break;
                case R.id.ll_drinkedcfy:
                    break;
                case R.id.ll_favoratecfy:
                    break;

            }
        }
    }

    void initViews(){
        httpUtils = new HttpUtils();
        jsonUtils = new JsonUtils();

        iv_coffee = findViewById(R.id.iv_bannerclassify);
        tv_headname = findViewById(R.id.tv_bnameheadclassify);
        tv_name = findViewById(R.id.tv_boriginclassify);
        tv_borigin = findViewById(R.id.tv_boriginclassify);
        tv_brand = findViewById(R.id.tv_brandclassify);
        tv_baking = findViewById(R.id.tv_bakingclassify);
        //tv_type = findViewById(R.id.tv_typeclassify);
        tv_score = findViewById(R.id.tv_numsocreclassify);
        tv_introduce = findViewById(R.id.tv_introduceclassify);
        tv_brief = findViewById(R.id.tv_briefIntroduce);
        lv_comment = findViewById(R.id.lv_commentclassify);
        ll_want = findViewById(R.id.ll_wantdrinkcfy);
        ll_drinked = findViewById(R.id.ll_drinkedcfy);
        ll_favorite = findViewById(R.id.ll_favoratecfy);
        star = new ImageView[5];
        star[0] = findViewById(R.id.img_star1cfy);
        star[1] = findViewById(R.id.img_star2cfy);
        star[2] = findViewById(R.id.img_star3cfy);
        star[3] = findViewById(R.id.img_star4cfy);
        star[4] = findViewById(R.id.img_star5cfy);
    }

    void setData(){
        coffee = (CoffeeBeanSearchList.DataDTO.ItemsDTO)getIntent().getSerializableExtra("coffeeInfo");
        setViews(coffee);

        //评论

        Call call = httpUtils.getCall("/coffee/comments/getCommentsByCoffee/"+coffee.getCoffeeId());

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                DetailedActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DetailedActivity.this, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                Log.e("res", res);
                DetailedActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        comments = jsonUtils.getComments(res).getData().getComments();
                        adapter = new CommentAdapter(comments, getBaseContext(), R.layout.classify_list_comment);
                        lv_comment.setAdapter(adapter);
                    }
                });
            }
        });
        adapter = new CommentAdapter();
        lv_comment.setAdapter(adapter);
    }

    void setViews(CoffeeBeanSearchList.DataDTO.ItemsDTO coffee1){
        Glide.with(this).load(coffee1.getCoffeeImg()).into(iv_coffee);
        tv_name.setText(coffee1.getCoffeeName());
        tv_introduce.setText(coffee1.getCoffeeIntroduce());
        setStar(coffee1.getCoffeeStar());
        tv_score.setText(coffee1.getCoffeeStar()+".0 分");
        tv_brief.setText(coffee1.getCoffeeBrief());

        String[] types = coffee1.getCoffeeTypes().split(",");
        if(types[types.length-1] != null){
            tv_brand.setText(types[types.length-1]);
        }else {
            tv_brand.setText("雀巢");
        }

        if (types[types.length-1] != null){
            tv_headname.setText(types[types.length-1]);
        }else{
            tv_headname.setText("阿拉比卡豆");
        }

        if(types[types.length-1] != null){
            tv_baking.setText(types[types.length-1]);
        }else{
            tv_baking.setText("中度烘焙");
        }

    }
}
